package com.itheima.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

@Slf4j
//@Aspect
//@Component
public class RecordTimeAspect {

    @Pointcut("execution(* com.itheima.service.impl.*.*(..))")
    private void pt(){}


    @Before("pt()")
    public void before(JoinPoint jp){
        String name = jp.getTarget().getClass().getName();
        String methodName = jp.getSignature().getName();
        log.info("{}.{}，before方法执行了",name,methodName);
    }


    @Around("pt()")
    public Object recordTime(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        Object proceed = pjp.proceed();
        String methodName = pjp.getSignature().getName();
        long end = System.currentTimeMillis();
        log.info("{}方法耗时：{}ms",methodName,end - start);
        return proceed;
    }


    @After("pt()")
    public void after(JoinPoint jp){
        String name = jp.getTarget().getClass().getName();
        String methodName = jp.getSignature().getName();
        log.info("{}.{}，after方法执行了",name,methodName);
    }

    @AfterReturning("pt()")
    public void afterReturning(JoinPoint jp){
        String name = jp.getTarget().getClass().getName();
        String methodName = jp.getSignature().getName();
        log.info("{}.{}，afterReturning方法执行了",name,methodName);
    }


    @AfterThrowing("pt()")
    public void afterThrowing(JoinPoint jp){
        String name = jp.getTarget().getClass().getName();
        String methodName = jp.getSignature().getName();
        log.info("{}.{}，afterThrowing方法执行了",name,methodName);
    }

}
